//计算天线值
export function getAnt(arr) {
  let binary = "";
  let temp = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
  temp.filter((val) => {
    if (arr.indexOf(val) > -1) {
      binary += 1;
    } else {
      binary += 0;
    }
  });
  return parseInt(binary.split("").reverse().join(""), 2);
}

//获取数据长度
export function getValueLen(data, multiple) {
  data = data.trim();
  return data.length % multiple == 0
    ? data.length / multiple
    : data.length / multiple + 1;
}

//十六进制字符串转字节数组
export function str2Bytes(str) {
  let pos = 0;
  let len = str.length;
  if (len % 2 != 0) {
    return null;
  }
  len /= 2;
  let hexA = new Array();
  for (let i = 0; i < len; i++) {
    let s = str.substr(pos, 2);
    let v = parseInt(s, 16);
    hexA.push(v);
    pos += 2;
  }
  return hexA;
}

//字节数组转十六进制字符串
export function bytes2Str(arr) {
  let str = "";
  for (let i = 0; i < arr.length; i++) {
    let tmp = arr[i].toString(16);
    if (tmp.length == 1) {
      tmp = "0" + tmp;
    }
    str += tmp;
  }

  return str;
}

//int转字节数组
export function value2Bytes(str) {
  str = parseInt(str);
  str = str.toString(16);
  let pos = 0;
  let len = str.length;
  if (len % 2 != 0) {
    str = "0" + str;
  }
  len /= 2;
  let bytes = new Array();
  for (let i = 0; i < len; i++) {
    let s = str.substr(pos, 2);
    let v = parseInt(s, 16);
    bytes.push(v);
    pos += 2;
  }
  return bytes;
}

//获取pc值
export function getPc(value) {
  let valueLen = parseInt(getValueLen(value, 4));
  let bytes = value2Bytes(valueLen << 11);
  return bytes2Str(bytes);
}

export function getGbPc(value) {
  let pc = null;
  let valueLen = parseInt(getValueLen(value, 4));
  if (valueLen < 10) {
    pc = "0" + valueLen + "00";
  } else if (valueLen < 100) {
    pc = valueLen + "00";
  }
  return pc;
}

export function padRight(value, multiple) {
  let len = parseInt(getValueLen(value, multiple)) * multiple;
  let length = value.length;
  while (length < len) {
    value = value + "0";
    length++;
  }
  return value;
}

// let anies = value2Bytes(1 << 11);
// console.log(anies);
// console.log(bytes2Str(anies));
// let str = "1234";
// for (let i = 0; i < 40; i++) {
//     console.log(getPc(str)+"----"+str);
//     str = str+"1234";
//
// }
let str =
  '{"baseOrApp":0,"data":{"baseSpeed":1,"cData":"AQQBAQ==","crc":"Mlg=","crcData":"AAECDAAA","dataLen":0,"inventoryFlag":1,"msgData":"WgABAgwABAEEAQE2jA==","msgType":{"msgId":12,"mt_12":"0","mt_13":"0","mt_14":"0","mt_15":"0","mt_8_11":"0010","pType":0,"pVersion":1},"qValue":4,"rs485Address":0,"rtCode":0,"rtMsg":"","session":1},"msgId":12,"result":0,"wid":4}';
// console.log(JSON.parse(str));
// console.log(padRight("12", 2));
